package com.itbz.JSP;

import com.itbz.pojo.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/*使用ForEach迭代Map
需求：

创建Users对象，含有userid，username属性。

创建一个Servlet，在Servlet中创建多个Users对象并放到Map集合中，
在showUsers2.jsp的页面中显示所有的Users对象的信息。*/
@WebServlet("/findUsers2.do")
public class FindUsersServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建测试数据   直接在构造方法中赋值
        Users users1 = new Users(1, "luce");
        Users users2 = new Users(2, "jack");

        //将数据 添加到Map  map结构是key-value
        Map<String,Users> map = new HashMap<>();
        map.put("users1",users1);
        map.put("users2",users2);

        //将map放进request对象中
        req.setAttribute("map",map);



        //通过请求转发进行跳转
        req.getRequestDispatcher("showUsers2.jsp").forward(req,resp);
    }
}
